﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using EntityModule;

namespace Sport
{
    public partial class Ajax : BasePage
    {
        [WebMethod()]
        public static void dataSave(string title)
        {
            string titlemain = title.Length > 140 ? title.Substring(0, 140) + "..." : title;
            New news = new New
            {
                Tilte = titlemain,
                Link = "http://www.minhquandev.com",
                DateModifier = DateTime.Now
            };
            NewsDetail detail = new NewsDetail
            {
                ID = Guid.NewGuid(),
                New = news,
                Body = title,
                DateModifier = DateTime.Now
            };
            new NewsService().Insert(news);
        }
        public void ProcessRequest(HttpContext context)
        {
            HttpPostedFile file1 = context.Request.Files["image"];
            string fileName = context.Request.Form["filename"];
            string filePath = "FileSave//";

            //write your handler implementation here.
            if (context.Request.Files.Count <= 0)
            {
                context.Response.Write("No file uploaded");
            }
            else
            {
                for (int i = 0; i < context.Request.Files.Count; ++i)
                {
                    HttpPostedFile file = context.Request.Files[i];
                    file.SaveAs(context.Server.MapPath(filePath + file.FileName));
                    context.Response.Write("File uploaded");
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string mode = Request.QueryString["mode"].AsString();
            System.Collections.Specialized.NameValueCollection value = Request.QueryString;
            if (mode == "login")
            {
                User user1 = Service.Users.FirstOrDefault(s => s.FBID == value["id"].AsString());
                if (user1 == null)
                {
                    User user = new User()
                        {
                            ID = Guid.NewGuid(),
                            FBID = value["id"].AsString()
                            ,
                            Name = value["name"].AsString()
                            ,
                            Email = value["email"].AsString()
                            ,
                            Gender = value["gender"].AsString()
                            ,
                            DateBirthday = value["birthday"].AsDate()
                        };

                    Service.Users.InsertOnSubmit(user);
                    Service.SubmitChanges();
                }
                else if (user1.StypeTheme != null)
                    Session[Constant.StyleTheme] = user1.StypeTheme;
            }
            else if (mode == "FileUpload")
            {
                string[] extension = new string[] { ".jpg", ".png", ".gif" };
                foreach (string file in Request.Files)
                {
                    HttpPostedFile hFile = Request.Files[file] as HttpPostedFile;
                    if (hFile.ContentLength > 0 && hFile.ContentLength < 2 * 1024 * 1024)
                        hFile.SaveAs(Server.MapPath("/ProductImages/") + hFile.FileName);
                }
            }
            else if (mode == "FileUpload")
            {
                string[] extension = new string[] { ".jpg", ".png", ".gif" };
                foreach (string file in Request.Files)
                {
                    HttpPostedFile hFile = Request.Files[file] as HttpPostedFile;
                    if (hFile.ContentLength > 0 && hFile.ContentLength < 2 * 1024 * 1024)
                        hFile.SaveAs(Server.MapPath("/ProductImages/") + hFile.FileName);
                }
            }
        }
    }
}